import 'dart:math';
import 'dart:ui';

import '../utils/color_utils.dart';
import 'base/base_effect.dart';
import 'effect_template.dart';

class Effect08 extends EditableAnimationEffect {
  Effect08(){
    {
      double sizeWidth = size.width;
      double sizeHeight =size.height;
      List<Map<int, int>> whiteList = [];
      for(int num = 0; num < 10; num++){
        whiteList.add(ColorUtils.getRandomWhiteItemMap(
            sizeWidth.toInt(),
            sizeHeight.toInt(),
            15 * sizeWidth.toInt() * sizeHeight.toInt() ~/ (24 * 24)));
      }
      this.whiteList = whiteList;
    }
  }

  List<Map<int, int>> whiteList = [];

  @override
  List<Color> get defaultColors {
    return const [
      Color(0xFF000000),
      Color(0xFFFFFF00),
      Color(0xFF00FF00),
      Color(0xFF00FFFF),
      Color(0xFF0000FF),
      Color(0xFFFF00FF)
    ];
  }

  @override
  bool get enableAngle => false;

  @override
  bool get enableColorWidth => false;

  @override
  bool get enableDensity => false;

  @override
  bool get enableEditColor => true;

  @override
  bool get enableSpeed => false;

  @override
  bool get isFixedColorCount => false;

  @override
  RangeArgument get colorCountRange => RangeArgument(2,6,6);

  @override
  EffectType get effectTyp => EffectType.effect08;

  @override
  Future<List<List<List<Color>>>> getFrames({EffectOperation? operation}) {

    List<Color> colorList = getColorList(effectOperation: operation);

    double sizeWidth = size.width;
    double sizeHeight = size.height;
    List<Color> newColorList =  [
      Color(0xFF000000),
      Color(0xFF000000),
      colorList.last,
      ...colorList,
      colorList.first,
      Color(0xFF000000),
      Color(0xFF000000)];

    int allFrame = ((newColorList.length.toDouble() / 2) * sizeHeight.toInt()).toInt() + sizeHeight.toInt().toInt();

    //平均记录各种颜色的渐变位置
    List<double> stops = [];
    double stop = 0;
    for(int i = 0; i < newColorList.length; i++) {
      if (stop > 1) {
        stop = 1;
      }
      stops.add(stop * (allFrame-1 + sizeHeight.toInt()-1));
      stop = stop + 1 / (newColorList.length - 1);
    }

    List<List<List<Color>>> frames = [];
    for (int i = 0; i < allFrame; i++) {
      Map<int, int> whiteItem = whiteList[i % whiteList.length];
      // Map<int, int> whiteItem = RandomDisappearItem.getRandomWhiteItemMap(sizeWidth.toInt(), sizeHeight.toInt(), 15);
      List<List<Color>> frame = [];
      for (int j = 0; j < sizeWidth.toInt(); j++) {
        int widthPosition = j % 10;
        List<Color> colors = [];
        for (int z = 0; z < sizeHeight.toInt(); z++) {
          int heightPosition = (sizeHeight.toInt() - 1 - z) % 10;
          if ((widthPosition >= heightPosition && widthPosition < heightPosition+5) || (widthPosition < heightPosition-5)) {
            int index = i + sizeHeight.toInt() - 1 - z;
            Color gradientColor = ColorUtils.lerpGradient(newColorList, stops, index.toDouble());
            // Color gradientColor = ColorUtils.lerpGradient(newColorList, stops, index.toDouble());
            colors.add(gradientColor);
          }
          else {
            if (z%5 == 0 && whiteItem[j*sizeWidth.toInt() + z] != null) {
              colors.add(ColorUtils.colorWithBrightness(Color(0xFFFFFFFF), Random().nextDouble()*0.8));
              // colors.add(ColorUtils.colorWithBrightness(Color(0xFFFFFFFF), Random().nextDouble()*0.8));
            }
            else {
              colors.add(Color(0xFF000000));
              // colors.add(Color(0xFF000000));
            }
          }
        }
        frame.add(colors);
      }
      frames.add(frame);
    }
    return Future.value(frames);
  }

  @override
  Size get size => const Size(24, 24);
}